package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 开户页
 * Created by Administrator on 2017/11/4.
 */

public class AlertPwdActivity extends BaseActivity implements View.OnClickListener {
    public static final int PWD_TYPE_LOGIN = 1;
    public static final int PWD_TYPE_TRAN = 2;

    private int pwdType = 0;
    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private EditText et_old_pwd;
    private EditText et_new_pwd1;
    private EditText et_new_pwd2;
    private TextView tv_hint;
    private TextView tv_sure;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd1 = (EditText) findViewById(R.id.et_new_pwd1);
        et_new_pwd2 = (EditText) findViewById(R.id.et_new_pwd2);
        tv_hint = (TextView) findViewById(R.id.tv_hint);
        tv_sure = (TextView) findViewById(R.id.tv_sure);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_alert_pwd);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        pwdType = getIntent().getIntExtra("pwdType", 0);
        if (pwdType == PWD_TYPE_LOGIN) {
            tv_hint.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_sure:
                submit();
                break;
        }
    }

    private void submit() {
        String oldPwd = et_old_pwd.getText().toString();
        String newPwd1 = et_new_pwd1.getText().toString();
        String newPwd2 = et_new_pwd2.getText().toString();
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtils.showShortToast(this, "请输入原密码");
            return;
        }
        if (TextUtils.isEmpty(newPwd1)) {
            ToastUtils.showShortToast(this, "请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(newPwd2)) {
            ToastUtils.showShortToast(this, "请确认新密码");
            return;
        }
        if (!newPwd1.equals(newPwd2)) {
            ToastUtils.showShortToast(this, "两次输入密码不一致");
            return;
        }
        mDialog.show();
        if (pwdType == PWD_TYPE_LOGIN) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("OldPwd", oldPwd);
                obj.put("NewPwd", newPwd1);
                PersonalNetWork.alertLoginPwd(UserTokenUtil.getToken(AlertPwdActivity.this), obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            ToastUtils.showShortToast(AlertPwdActivity.this, "修改密码成功");
                            back();
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                ToastUtils.showShortToast(AlertPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(AlertPwdActivity.this, "修改密码失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (pwdType == PWD_TYPE_TRAN) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("accountNumber", UserTokenUtil.getUserName(AlertPwdActivity.this));
                obj.put("prePassword", oldPwd);
                obj.put("newPassword", newPwd1);
                PersonalNetWork.alertTranPwd(UserTokenUtil.getToken(AlertPwdActivity.this), obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            ToastUtils.showShortToast(AlertPwdActivity.this, "修改密码成功");
                            back();
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                ToastUtils.showShortToast(AlertPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(AlertPwdActivity.this, "修改密码失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }
}
